package com.hsnn.medstgmini.pro.base.validator;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.hsnn.medstgmini.pro.base.model.BaseBakpurchase;
import com.hsnn.medstgmini.validator.Result;

/**
 * @category 医院备案采购申请 字段校验
 * @author 韩守松
 * @date 2015年7月14日
 */
public class BaseBakpurchaseYYValidator {
	private static final Logger log = Logger
			.getLogger(BaseBakpurchaseYYValidator.class);

	public static Result validate(BaseBakpurchase baseBakpurchase) {
		Result validate = new Result();
		Map<String, List<String>> failCauseMap = new HashMap<String, List<String>>();
		List<String> failDescriptionList = new ArrayList<String>();
		boolean falg = false;
		DecimalFormat df = new DecimalFormat("0.00");
		String regPrice = "^[0-9]+(.[0-9]{2})?$";// 校验采购价格格式(有两位小数的正实数)
		String regNum = "^\\+?[1-9][0-9]*$";// 校验采购数量格式(非零的正整数)
		try {
			// 校验配送企业
			if (StringUtils.isNotEmpty(baseBakpurchase.getDelCode())) {
				failDescriptionList.add("");
				failCauseMap.put("配送企业错误", failDescriptionList);
				validate.isSuccess();
			} else {
				failDescriptionList.add("配送企业不能为空");
				failCauseMap.put("配送企业错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);

			// 校验采购价格
			failDescriptionList = new ArrayList<String>();
			if (null != baseBakpurchase.getPurPrice()
					&& !"".equals(baseBakpurchase.getPurPrice())) {
				if (Pattern
						.compile(regPrice)
						.matcher(
								df.format(baseBakpurchase.getPurPrice())
										.toString()).matches()) {
					failDescriptionList.add("");
					failCauseMap.put("采购价格错误", failDescriptionList);
					validate.isSuccess();
				} else {
					failDescriptionList.add("采购价格为含两位小数的正整数数字型数据");
					failCauseMap.put("采购价格错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			} else {
				failDescriptionList.add("采购价格不能为空");
				failCauseMap.put("采购价格错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);

			// 校验采购数量
			failDescriptionList = new ArrayList<String>();
			if (null != baseBakpurchase.getPurAmount()
					&& !"".equals(baseBakpurchase.getPurAmount())) {
				if (Pattern.compile(regNum)
						.matcher(baseBakpurchase.getPurAmount().toString())
						.matches()) {
					failDescriptionList.add("");
					failCauseMap.put("采购数量错误", failDescriptionList);
					validate.isSuccess();
				} else if (0 == baseBakpurchase.getPurAmount()) {
					failDescriptionList.add("采购数量必须大于零");
					failCauseMap.put("采购数量错误", failDescriptionList);
					validate.setSuccess(falg);
				} else {
					failDescriptionList.add("采购数量必须大于零的整数的数字");
					failCauseMap.put("采购数量错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			} else {
				failDescriptionList.add("采购价格不能为空");
				failCauseMap.put("采购采购错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);
			// 校验备案原因
			failDescriptionList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(baseBakpurchase.getBakReason())) {
				if ("4".equals(baseBakpurchase.getBakReason())) {
					if (baseBakpurchase.getBakReason().length() <= 512) {
						failDescriptionList.add("");
						failCauseMap.put("备案原因错误", failDescriptionList);
						validate.isSuccess();
					} else {
						failDescriptionList.add("备案原因最多512个字");
						failCauseMap.put("备案原因错误", failDescriptionList);
						validate.setSuccess(falg);
					}
				}
			} else {
				failDescriptionList.add("备案原因不能为空");
				failCauseMap.put("备案原因错误", failDescriptionList);
				validate.setSuccess(falg);
			}
			validate.setFailCauseMap(failCauseMap);

			// 校验备案备注
			failDescriptionList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(baseBakpurchase.getRemark())) {
				if (baseBakpurchase.getRemark().length() <= 1024) {
					failDescriptionList.add("");
					failCauseMap.put("备案备注错误", failDescriptionList);
					validate.isSuccess();
				} else {
					failDescriptionList.add("备案备注最多1024个字");
					failCauseMap.put("备案备注错误", failDescriptionList);
					validate.setSuccess(falg);
				}
			}
			validate.setFailCauseMap(failCauseMap);
			return validate;
		} catch (Exception e) {
			log.error("校验信息出错-->" + e);
			validate.setSuccess(falg);
			return validate;
		}
	}
}
